package com.gxa.community.admin.controller;

import com.gxa.community.core.utils.R;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.AppendObjectRequest;
import com.qcloud.cos.model.AppendObjectResult;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.region.Region;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@Slf4j
@RestController
public class CosController {
    /**
     * 腾讯云上传文件
     */
    private String accessKey = "AKIDmOlsiHZaJmytXu0oLbaJlvpRmqc7LIyN";
    private String secretKey = "8zZsuf9OM3SaS3NRFm1zh9KDpBG3ZpnU";
    private String regionName = "ap-chengdu";
    private String bucketName = "king-1252278080";
    private String keyName = "/files";

    /**
     * 上传文件
     * @param multipartFile 上传文件
     * @param request
     * @param response
     * @return R
     */
    @PostMapping("/file")
    public R file(@RequestParam("multipartFile")MultipartFile multipartFile, HttpServletRequest request, HttpServletResponse response){
        COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
        ClientConfig clientConfig = new ClientConfig(new Region(regionName));
        COSClient cosclient = new COSClient(cred, clientConfig);
        // 获取文件类型
        String name = multipartFile.getOriginalFilename();
        String fileType = name.substring(name.lastIndexOf(".") + 1);
        // bucket名需包含appid
        String key = keyName + "/" + UUID.randomUUID() + "." + fileType;
        String url = null;
        try {
            // 处理文件路径
            String filePath = request.getSession().getServletContext().getRealPath("/") + multipartFile.getOriginalFilename();
            multipartFile.transferTo(new File(filePath));
            File localFile = new File(filePath);
            // 报错请求对象
            AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, localFile);
            // 设置节点
            appendObjectRequest.setPosition(0L);
            AppendObjectResult appendObjectResult = cosclient.appendObject(appendObjectRequest);
            // 文件大小
            long nextAppendPosition = appendObjectResult.getNextAppendPosition();
            log.info("文件大小：{}", nextAppendPosition);
            // 获取返回对象
            GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
            COSObject cosObject = cosclient.getObject(getObjectRequest);
            url = cosObject.getObjectContent().getHttpRequest().getURI().toString();
        } catch (CosServiceException e) {
            e.printStackTrace();
        } catch (CosClientException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            cosclient.shutdown();
        }
        return R.ok().put("url",url);
    }
}
